package org.springframework.howelly.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.springframework.binding.message.MessageContext;
import org.springframework.howelly.validators.ObraValidator;

@Entity
public class Obra implements Serializable {

    private static final long serialVersionUID = 4011346719502656269L;

    private Long id;

    private String name;

    private String address;

    private String city;

    private String state;

    private String zip;

    private String country;

    // prices
    private BigDecimal price;

    private BigDecimal saldo = BigDecimal.ZERO;

    // Dates
    private Date startDate;

    private Date finishDate;

    private Collection<Gasto> gastos;

    private Collection<Categoria> categorias = new ArrayList<Categoria>();

    public Obra() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getAddress() {
	return address;
    }

    public void setAddress(String address) {
	this.address = address;
    }

    public String getCity() {
	return city;
    }

    public void setCity(String city) {
	this.city = city;
    }

    public String getZip() {
	return zip;
    }

    public void setZip(String zip) {
	this.zip = zip;
    }

    public String getState() {
	return state;
    }

    public void setState(String state) {
	this.state = state;
    }

    public String getCountry() {
	return country;
    }

    public void setCountry(String country) {
	this.country = country;
    }

    public BigDecimal getPrice() {
	return price;
    }

    public void setPrice(BigDecimal price) {
	this.price = price;
    }

    public Gasto createBooking(User user) {
	return new Gasto(this, user);
    }

    @Override
    public String toString() {
	return "Obra(" + name + "," + address + "," + city + "," + zip + ")";
    }

    public BigDecimal getSaldo() {
	return saldo;
    }

    public void setSaldo(BigDecimal saldo) {
	this.saldo = saldo;
    }

    public Date getStartDate() {
	return startDate;
    }

    public void setStartDate(Date startDate) {
	this.startDate = startDate;
    }

    public Date getFinishDate() {
	return finishDate;
    }

    public void setFinishDate(Date finishDate) {
	this.finishDate = finishDate;
    }

    @OneToMany(mappedBy = "obra", targetEntity = Gasto.class, fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    public Collection<Gasto> getGastos() {
	return gastos;
    }

    @OneToMany(mappedBy = "obra", targetEntity = Categoria.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public Collection<Categoria> getCategorias() {
	return categorias;
    }

    public void setCategorias(Collection<Categoria> categorias) {
	this.categorias = categorias;
    }

    public void setGastos(Collection<Gasto> gastos) {
	this.gastos = gastos;
    }

    // Validators
    public void validateNewObra(MessageContext context) {
	ObraValidator.validateNewObra(context, this);
    }

    public void validateDeleteObra(MessageContext context, Obra obra) {
	// ObraValidator.validateDeleteObra(context, obra);
    }

}
